<template>
	<view class="content">
		<!-- <view class="avator">
			<image v-if="need" @click="changeAvator" :src="baseUrl + info.logo" mode="aspectFill"></image>
			<image v-else @click="changeAvator" :src="info.logo" mode="aspectFill"></image>
		</view> -->
		<button class="avator" open-type="chooseAvatar" @chooseavatar="onChooseavatar">
			<image v-if="need" :src="baseUrl + info.logo" mode="aspectFill"></image>
			<image v-else :src="info.logo" mode="aspectFill"></image>
		</button>
		<view class="name">
			<text>用户昵称：</text>
			<input type="text" v-model="info.name" />
		</view>
		<view class="submit" @click="save">保存修改</view>
	</view>
</template>

<script>
	import urlConfig from '@/common/config.js'
	import {
		udpAccount
	} from '@/api/user.js'
	export default {
		data() {
			return {
				info: {},
				phone: '',
				baseUrl: this.$baseUrl,
				need: true
			}
		},
		onLoad() {
			this.info = uni.getStorageSync('userInfo')
			if(this.info.logo !== null && this.info.logo.indexOf('http') !== -1) {
				this.need = false
			} else {
				this.need = true
			}
			
			if(this.info.name === '' || this.info.name === null || this.info.name === undefined) {
				this.info.name = '微信用户'
			}
			this.phone = uni.getStorageSync('phone')
			console.log('phone', this.phone)
		},
		methods: {
			changeAvator() {
				let that = this
				uni.chooseImage({
					count: 1,
					success: res => {
						uni.uploadFile({
							url: urlConfig + '/public/file',
							filePath: res.tempFilePaths[0],
							name: 'file',
							success: (inner) => {
								let str = JSON.parse(inner.data)
								that.info.logo = str.data.url
								if(that.info.logo.indexOf('http') !== -1) {
									that.need = false
								} else {
									that.need = true
								}
								that.$forceUpdate()
							}
						})
					}
				})
			},
			onChooseavatar(e) {
				let that = this
				uni.uploadFile({
					url: urlConfig + '/public/file',
					filePath: e.detail.avatarUrl,
					name: 'file',
					success: (inner) => {
						let str = JSON.parse(inner.data)
						that.info.logo = str.data.url
						if(that.info.logo.indexOf('http') !== -1) {
							that.need = false
						} else {
							that.need = true
						}
						that.$forceUpdate()
					}
				})
			},
			save() {
				let data = {
					phone: this.phone,
					email: this.email || '',
					name: this.info.name,
					logo: this.info.logo,
					birth: this.birth || ''
				}
				console.log(data)
				udpAccount(data).then(res => {
					uni.showToast({
						title: res.message,
						icon: 'none'
					})
					uni.setStorageSync('userInfo', this.info)
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/my/my'
						})
					}, 800)
				})
			},
		}
	}
</script>

<style lang="scss">
.content {
	padding: 30rpx 0;
	background-color: #F8F9FA;
	min-height: 100vh;
}
.avator {
	padding: 60rpx 0;
	text-align: center;
	image {
		width: 160rpx!important;
		height: 160rpx!important;
		border-radius: 50%;
	}
}
.name {
	display: flex;
	justify-content: space-between;
	align-items: center;
	padding: 60rpx 30rpx;
	background-color: #fff;
	input {
		text-align: right;
	}
}
.submit {
	margin: 60rpx 30rpx;
	line-height: 80rpx;
	text-align: center;
	color: #fff;
	background-color: #C10016;
	border-radius: 80rpx;
}
button {
	&::after {
		border: none;
	}
}
</style>
